<script>
  let o = {
    a: 1,
    b: {
      c: 2,
      d: 3,
      e: {
        f: 100,
        g: {
          h: 200,
        },
      },
    },
  };
  //   o.b.e.f = 999999;
  let o2 = o;
  o2.b.e.f = 99999;
  // console.log(o);
  //这边o2改变，o也跟着改变
  //因为o2和o不是基本类型，都是引用，他们共用一个内存值，所以必会改变
  //所以需要深拷贝
  /**
   *  深拷贝
   */
  function deepClone(obj = {}) {
    //首先判断是不是null，以及不是对象和不是数组，直接return
    if (typeof obj !== 'object' || obj == null) {
      return obj;
    }
    //判断他是数组还是对象
    let result = null;
    if (obj instanceof Array) {
      result = [];
    } else {
      result = {};
    }
    console.log(obj);
    for (const key in obj) {
      console.log(key);
      if (obj.hasOwnProperty(key)) {
        result[key] = deepClone(obj[key]);
      }
    }
    return result;
  }

  let obj1 = {
    name: 'djw',
    arr: [1, 2, 3],
    info: {
      address: 'shanghai',
    },
  };
  let obj2 = deepClone(obj1);
  // console.log(obj2);
  obj2.arr[0] = 99;
  obj2.info.address = 'beiji';
  // console.log(obj1);
</script>
